<?php

require_once __CFG_PATH_LIBS . __CFG_PATH_CORE."mail.php";

$ErrorMessages = array();

if($CONFIG['SiteTypeCode'] == 'skwcom')
{
  $ThemeTableParts['db'] = $CLIENT['sitelanguage'];
  $ThemeTableParts['cat_categ'] = $CLIENT['config']['catalog_category_code'];
  $ThemeTableParts['cat_subcateg'] = $CLIENT['config']['catalog_subcategory_code'];
  $ThemeTableName = getThemeTableNameFull($ThemeTableParts);

  $Theme['subcategory'] = getFieldByEnother('title'.__FLANG, $_SQL_TABLE['catalog_subcategory'], 'id', $CLIENT['config']['catalog_subcategory']);
  $Theme['theme'] = getFieldByEnother('title', $ThemeTableName, 'id', $CLIENT['config']['seo_theme']);
  $tpl->assign("Theme", $Theme);
}

if(!isEmptyArr($_POST) && $_POST['task'] == 'send')
{  if(!preg_match("/^".$CONFIG['emailPattern']."$/", $_POST['email']))
  {
  	$ErrorMessages[] = $ERRORS['tellFriend'][__ERROR_INCORRECT_EMAIL];
  }
  if(!preg_match("/^".$CONFIG['emailPattern']."$/", $_POST['friend_email']))
  {
  	$ErrorMessages[] = $ERRORS['tellFriend'][__ERROR_INCORRECT_FRIEND_EMAIL];
  }
  if(isBlank($_POST['name']) || isBlank($_POST['friend_name']))
  {
  	$ErrorMessages[] = $ERRORS['tellFriend'][__ERROR_EMPTY_REQUIRED_FIELDS];
  }
  if(isBlank($_POST['code']) || $_POST['code'] != getSecurityNumber())
  {
  	$ErrorMessages[] = $ERRORS['tellFriend'][__ERROR_INCORRECT_SECURITY_CODE];
  }
  if(isEmptyArr($ErrorMessages))
  {    $tpl->assign("User", $_POST);
    $tpl->template_dir = __CFG_PATH_MAIL_TEMPLATE;
	$mailBody = $tpl->fetch("mail.tell_a_friend_".$CONFIG['SiteTypeCode'].".tpl");
	$tpl->template_dir = __CFG_PATH_TEMPLATE;
    sendMailToUser($_POST['friend_email'], $MailSubject['tellFriend']['tellFriendForm'], $mailBody, null, $_POST['name'], $_POST['email']);

    $toDBArr['name'] = $_POST['name'];
	$toDBArr['email'] = $_POST['email'];
    $toDBArr['client_domain'] = __CFG_CLIENT_ID;
    $toDBArr['language'] = $CLIENT['sitelanguage'];
    userSubscribe($toDBArr, $Errors);
    /*makeInsertList($strColumns,$strValues, $toDBArr, array('date'));
    insertItem($_SQL_TABLE['subscriber'], $strColumns, $strValues);*/

    $toDBArr['name'] = $_POST['friend_name'];
	$toDBArr['email'] = $_POST['friend_email'];
	userSubscribe($toDBArr, $Errors);
    /*makeInsertList($strColumns,$strValues, $toDBArr, array('date'));
    insertItem($_SQL_TABLE['subscriber'], $strColumns, $strValues);*/

    showMessageAlertJVSC($language['tellFriend']['thankYouForYourActivity']);
    go_toJVSC(getPath()."tell-a-friend".$CONFIG['webPageFileType']);
    exit;
  }
}

prepareSecurityNumber();
if($USER['isLogin'] > 0 && isEmptyArr($_POST))
{  $_POST['name'] = $USER['info']['firstname'].' '.$USER['info']['lastname'];
  $_POST['email'] = $USER['info']['email'];
}
$User['friend_name'] = '<span id="friend_name">'.(($_POST['friend_name'] == '') ? strToUpperCase($language['tellFriend']['friendsName']) : $_POST['friend_name']).'</span>';
$User['name'] = '<span id="your_name">'.(($_POST['name'] == '') ? strToUpperCase($language['tellFriend']['yourNameGoHere']) : $_POST['name']).'</span>';
$User['email'] = '<span id="your_email">'.(($_POST['email'] == '') ? strToUpperCase($language['tellFriend']['yourEmailGoHere']) : $_POST['email']).'</span>';

$toRepl = array('sitename' => $CONFIG['SiteName']);
replaceVariables($toRepl, $language['tellFriend']['thankYouForRecommending']);

$tpl->assign("User", $User);
$tpl->assign("ActiveThemes", $ActiveThemes);
$tpl->assign("NotUseInclude", true);
$tpl->assign("Theme", $Theme);
$tpl->template_dir = __CFG_PATH_MAIL_TEMPLATE;
$mailBody = $tpl->fetch("mail.tell_a_friend_".$CONFIG['SiteTypeCode'].".tpl");
$tpl->template_dir = __CFG_PATH_TEMPLATE;
$tpl->assign("User", $USER);
$tpl->assign("POST", $_POST);
$tpl->assign("mailBody", $mailBody);
$tpl->assign("ErrorMessages", $ErrorMessages);
$tpl->assign("task", $_POST['task']);

$tpl->assign("PageTitle", $language['tellFriend']['tellAFriend']);

$_BODY_SMARTY_TEMPLATE = $CONFIG['ModulesFolder']."module.tell_a_friend.tpl";

?>